package com.pinusmusic.dao.impl;

import com.pinusmusic.dao.FavoriteDao;
import com.pinusmusic.domain.*;

import com.pinusmusic.util.JDBCUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class FavoriteDaoImpl implements FavoriteDao {

    JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public boolean isUid(String uid) {
        boolean flag=false;
        String sql="select username from user where uid=?";
        try {
            template.queryForObject(sql,String.class,uid);
        }catch (EmptyResultDataAccessException e) {
            return flag;
        }
        flag=true;
        return flag;
    }

    @Override
    public boolean isMid(String mid) {
        boolean flag=false;
        String sql="select musicname from music where mid=?";
        try {
            template.queryForObject(sql,String.class,mid);
        }catch (EmptyResultDataAccessException e) {
            return flag;
        }
        flag=true;
        return flag;
    }

    @Override
    public boolean isFid(String fid) {
        boolean flag=false;
        String sql="select uid from favorite where fid=?";
        try {
            template.queryForObject(sql,Integer.class,fid);
        }catch (EmptyResultDataAccessException e) {
            return flag;
        }
        flag=true;
        return flag;
    }

    @Override
    public boolean addFavorite(Favorite favorite) {
        boolean flag=false;
        String sql="insert into favorite values (null,?,?)";
        if (template.update(sql,favorite.getUid(),favorite.getMid())!=0){
            flag=true;
        }
        return flag;
    }

    @Override
    public boolean deleteFavorite(int fid) {
        boolean flag=false;
        String sql="delete from favorite where fid=?;";
        if (template.update(sql,fid)!=0){
            flag=true;
        }
        return flag;
    }

    @Override
    public List<MusicInfo> findFavoriteMusicsByUid(int uid) {
        String sql="select * from favorite where uid=?";
        System.out.println("uid="+uid);
        List<Favorite> favoriteList=template.query(sql,new BeanPropertyRowMapper<Favorite>(Favorite.class),uid);
        List<MusicInfo> musicList=new ArrayList<>();

        String MusicSql="select * from music where mid=?";
        String SingerSql="select * from Singer where sid=?";
        for (Favorite favorite:favoriteList) {
            MusicInfo musicInfo=new MusicInfo();
            musicInfo.setMusic(template.query(MusicSql, new BeanPropertyRowMapper<>(Music.class), favorite.getMid()).get(0));
            musicInfo.setSinger(template.query(SingerSql, new BeanPropertyRowMapper<>(Singer.class), musicInfo.getMusic().getSid()).get(0));
            musicList.add(musicInfo);
        }
        return musicList;
    }

    @Override
    public void deleteFavByMid(int mid) {
        String sql = "delete from favorite where mid = ?";
        template.update(sql,mid);
    }

}
